/*******************************************************************************
 * Copyright (c) 2006 CEA List.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     CEA List - initial API and implementation
 *******************************************************************************/
package com.cea.papyrus.diagram.statemachine.editpart;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.draw2d.IFigure;
import org.eclipse.draw2d.geometry.Dimension;
import org.eclipse.draw2d.geometry.Point;
import org.eclipse.draw2d.geometry.Rectangle;
import org.eclipse.emf.common.notify.Notifier;
import org.eclipse.gef.DragTracker;
import org.eclipse.gef.EditPart;
import org.eclipse.gef.GraphicalEditPart;
import org.eclipse.gef.Request;
import org.eclipse.uml2.uml.ConnectionPointReference;
import org.eclipse.uml2.uml.Element;
import org.eclipse.uml2.uml.ElementImport;

import com.cea.papyrus.core.editor.ModelManager;
import com.cea.papyrus.core.editpart.DiagramEditPart;
import com.cea.papyrus.core.editpart.GraphNodeWithSemanticEditPart;
import com.cea.papyrus.core.editpart.LinkNameDisplayEditPart;
import com.cea.papyrus.core.editpart.connection.AnchorMngr;
import com.cea.papyrus.core.editpart.connection.EllipseAnchorMngr;
import com.cea.papyrus.core.figures.ElementFigure;
import com.cea.papyrus.core.utils.Debug;
import com.cea.papyrus.diagram.statemachine.figure.ConnectionPointReferenceFigure;
import com.cea.papyrus.diagram.statemachine.helper.NodeConnectionPointReferenceHelper;
import com.cea.papyrus.diagraminterchange2.di2.GraphNode;
import com.cea.papyrus.diagraminterchange2.di2.Uml1SemanticModelBridge;

/**
 * Controller for a {@link ConnectionPointReference}.
 */
public class ConnectionPointReferenceEditPart extends GraphNodeWithSemanticEditPart {

	/** associated name editpart */
	protected  LinkNameDisplayEditPart nameEditpart;

	/**
	 * Creates a new ConnectionPointReferenceEditPart
	 * @param graphNode the graph node representing the connection point reference
	 * @param modelManager the model manager associated to the editor
	 */
	public ConnectionPointReferenceEditPart(GraphNode graphNode,ModelManager modelManager) {
		super(graphNode,modelManager);
	}

	/**
	 * Creates the anchor manager.
	 * 
	 * @return the anchor mngr
	 */
	@Override
	protected AnchorMngr createAnchorManager() {
		return new EllipseAnchorMngr(this);
	}


	/* (non-Javadoc)
	 * @see com.cea.papyrus.common.editpart.GraphNodeWithSemanticEditPart#getNodeFigure()
	 */
	@Override
	protected ElementFigure getNodeFigure(){
		return (ElementFigure)createFigure() ;
	}

	/* (non-Javadoc)
	 * @see com.cea.papyrus.common.editpart.GraphNodeWithSemanticEditPart#createFigure()
	 */
	@Override
	protected IFigure createFigure() {
		return new ConnectionPointReferenceFigure();	
	}

	/* (non-Javadoc)
	 * @see org.eclipse.gef.editparts.AbstractEditPart#getModelChildren()
	 */
	@Override
	protected List getModelChildren() { //look for all graphnodes, 
		ArrayList contained= new ArrayList();//that is associated to label

		// 	contained.add(getGraphNode().getContained().get(0));
		return contained;
	}

	/* (non-Javadoc)
	 * @see org.eclipse.gef.editparts.AbstractEditPart#addChild(org.eclipse.gef.EditPart, int)
	 */
	@Override
	protected void addChild(EditPart child, int index) {
		if (child.equals(nameEditpart)){
			((DiagramEditPart)getParent()).addExplicitEditpart(nameEditpart, index);
		}
		else {
			super.addChild(child, index);
		}
	}

	/* (non-Javadoc)
	 * @see org.eclipse.gef.editparts.AbstractGraphicalEditPart#getDragTracker(org.eclipse.gef.Request)
	 */
	@Override
	public DragTracker getDragTracker(Request request) {
		return new ConnectionPointReferenceDragTracker(this);
	}

	/* (non-Javadoc)
	 * @see com.cea.papyrus.common.editpart.GraphNodeWithSemanticEditPart#activate()
	 */
	@Override
	public void activate() {
		if (isActive()) {
			Debug.debug(this, "should not activate an already active edit part", channel);
			return;
		}
		// start listening for changes in the model
		((Notifier)getGraphNode()).eAdapters().add(_adapter);
		super.activate();
	}

	/* (non-Javadoc)
	 * @see com.cea.papyrus.common.editpart.GraphNodeWithSemanticEditPart#deactivate()
	 */
	@Override
	public void deactivate() {
		if (!isActive()) {
			Debug.debug(this, "should not deactivate an already deactived edit part", channel);
			return;
		}
		// stop listening for changes in the model
		((Notifier)getGraphNode()).eAdapters().remove(_adapter);
		super.deactivate();
	}

	/* (non-Javadoc)
	 * @see com.cea.papyrus.common.editpart.GraphNodeWithSemanticEditPart#refreshVisuals()
	 */
	@Override
	protected void refreshVisuals(){
		Dimension size = getGraphNode().getSize();
		Point p = getGraphNode().getPosition();
		Rectangle r = new Rectangle(p ,size);

		ConnectionPointReferenceFigure fig = (ConnectionPointReferenceFigure)getFigure();
		if(getUmlElement().getEntries().isEmpty() && getUmlElement().getExits().isEmpty())
			fig.setKind(0);
		else if(!getUmlElement().getEntries().isEmpty())
			fig.setKind(1);
		else if(!getUmlElement().getExits().isEmpty())
			fig.setKind(2);

		((GraphicalEditPart) getParent()).setLayoutConstraint(this, fig, r);
	}

	/* (non-Javadoc)
	 * @see com.cea.papyrus.common.editpart.GraphNodeWithSemanticEditPart#getUmlElement()
	 */
	@Override
	public ConnectionPointReference getUmlElement() {
		Element e = ((Uml1SemanticModelBridge)getGraphNode().getSemanticModel()).getElement();
		//may be an Element import
		if(e instanceof ElementImport)
			return (ConnectionPointReference)((ElementImport)e).getImportedElement();
		return ((ConnectionPointReference)e);
	}

	/**
	 * Calculates the good position of a connection point reference by taking account his parent.
	 * @param point the position of the current connection point reference in absolute coordinate
	 * @return the new position of the connection point reference.
	 */
	public Point inRunnerOfClass(Point point){
		Dimension connectionPointRefDimension = getGraphNode().getSize();
		//apply good position , the default is the same point
		//FIXME should be a nodehelper called from the plugin
		NodeConnectionPointReferenceHelper np = new NodeConnectionPointReferenceHelper();
		return np.inRunner((GraphNode)getGraphNode().getSemanticParent(), point, connectionPointRefDimension);
	}

    /**
     * Return false.
     * This method is used in {@link com.cea.papyrus.common.editpart.LayoutEditPolicy} in order to
     * know how to compute the coordinate of a added part.
     * @TODO move this method to more appropriate place. 
     * @return
     */
    @Override
    public boolean needRelativeTranslationForAddCommand()
    {
        return false;
    }

}
